package JasPerl::JSTL::Fmt::SetLocaleTag;

use 5.010;
use strict;
use warnings;

use parent qw(JasPerl::TagExt::JspTag);

use JasPerl::JSTL::Core::Config qw(:fmt);
use JasPerl::Util::Locale;

sub set_scope {
    $_[0]->{scope} = JasPerl::PageContext->get_scope_by_name($_[1]);
}

sub set_value {
    $_[0]->{value} = $_[1];
}

sub set_variant {
    $_[0]->{variant} = $_[1];
}

sub do_tag {
    my $self = shift;
    my $context = $self->get_jsp_context();

    my $locale;
    if (ref $self->{value}) {
        $locale = $self->{value};
    } elsif (length $self->{value}) {
        my ($language, $country) = split(/[_-]/, $self->{value}, 2);
        $locale = JasPerl::Util::Locale->new($language, $country, $self->{variant});
    } else {
        $locale = JasPerl::Util::Locale->get_default();
    }

    JasPerl::JSTL::Core::Config->set($context, FMT_LOCALE, $locale, $self->{scope});

    # TODO: session var? in LocalizationContext
    my $response = $context->get_response();
    $response->set_locale($context, $locale);
}

1;
